import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2023-10-25
 * Time: 9:45
 */
//测试ObjectMapper类
class Student {
    public String StudentId;
    public String ClassId;
}

public class ObjectServlet {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        //readValue 是把json格式的字符串，转化为类对象
        String s = "{\"StudentId\":4123,\"ClassId\":106}";
        Student student = objectMapper.readValue(s, Student.class);
        System.out.println("studentId: " + student.StudentId);
        System.out.println("classId: " + student.ClassId);

        //writeValue | writeValueAsString 将类对象转化为json格式字符串
        Student stu = new Student();
        stu.StudentId = "198";
        stu.ClassId = "111";
        String str = objectMapper.writeValueAsString(stu);
        System.out.println(str);
    }

}
